# -*- coding:utf-8 -*-

"""Widget de geolocalisation pour pyGTK."""

import pygtk
pygtk.require('2.0')
import gtk

import os.path
CWD = os.path.dirname(__file__)
MAP_URI = "file://" + CWD + "/gmap.html"
WEB_SIZE = (300, 250)


#------------------------------------------------------------------------------

class GeoMap:
    def __init__(self):
        print "GeoTag: ERROR - no backend selected for GeoMap!"


#------------------------------------------------------------------------------

class WebWrapper():
    """Carte avec marqueur (interface generique)."""
    
    def initialize(self):
        """Initialisation generique."""
        self.set_size_request(WEB_SIZE[0]+17, WEB_SIZE[1]+17)
        self.coords = None
        self.nccb = None
        return
    
    def load_map(self, c=None):
        """Charge une carte en fonction des coordonnées GPS."""
        self.stop()
        p = "" if (c == None) else "?"+c[0]+","+c[1]
        self.open_url(MAP_URI + p)
        return
    
    def load_blank(self):
        """Affiche une page blanche."""
        self.stop()
        self.coords = None
        self.open_url("about:blank")
        return
    
    def set_new_coords_callback(self, cb):
        """Specifie la fonction a executer quand les coordonnees changent."""
        self.nccb = cb
        return
    
    def on_map_clic_2(self, c):
        """Gestion du clic sur la carte (= changement de titre)."""
        self.coords =  c if (len(c) == 2) else None
        if self.nccb != None:
            self.nccb()
        return


#------------------------------------------------------------------------------

def set_web_backend(b):
    """Selection du moteur web a utiliser ('mozilla' ou 'webkit')."""
    global GeoMap
    
    if b == "mozilla":
        import gtkmozembed
        class GeoMapMozilla(gtkmozembed.MozEmbed, WebWrapper):
            def __init__(self):
                gtkmozembed.MozEmbed.__init__(self)
                self.initialize()
                self.connect('title', lambda d: self.on_map_clic(d))
                self.open_url = self.load_url
                self.stop = self.stop_load
            def on_map_clic(self, data=None):
                c =  self.get_title().split()
                self.on_map_clic_2(c)
        GeoMap = GeoMapMozilla
        return True
    
    elif b == "webkit":
        import webkit
        class GeoMapWebkit(webkit.WebView, WebWrapper):
            def __init__(self):
                webkit.WebView.__init__(self)
                s = self.get_settings()
                ###s.set_property('enable-dns-prefetching', False)
                s.set_property('enable-html5-database', False)
                s.set_property('enable-html5-local-storage', False)
                s.set_property('enable-java-applet', False)
                s.set_property('enable-offline-web-application-cache', False)
                s.set_property('enable-plugins', False)
                s.set_property('enable-private-browsing', True)
                s.set_property('enable-scripts', True)
                s.set_property('javascript-can-open-windows-automatically', False)
                self.initialize()
                self.connect('title-changed', self.on_map_clic)
                self.open_url = self.load_uri
                self.stop = self.stop_loading
            def on_map_clic(self, view, frame, title):
                c = title.split() if (title) else ""
                self.on_map_clic_2(c)
        GeoMap = GeoMapWebkit
        return True
    
    else:
        return False


#------------------------------------------------------------------------------


